<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/layui/css/layui.css" media="all">
    <script src="../../static/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../static/javascript/jquery.min.js"></script>

</head>
<script>
    $(document).ready(function () {

    });
</script>

<body>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>添加学生</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">学生姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="studentName" id="studentName" autocomplete="off" lay-verify="title" placeholder="请输入学生名字"
                    class="layui-input">
            </div>
             <div class="layui-form-item">
            <label class="layui-form-label">班级</label>
            <div class="layui-input-inline">
                <input type="text" name="className" id="className" autocomplete="off"  placeholder="请输入班级名"
                       class="layui-input">
            </div>
               </div>
        <div class="layui-form-item">
            <label class="layui-form-label">学生学号</label>
            <div class="layui-input-inline">
                <input type="text" name="sno" id="sno" placeholder="请输入学生学号" lay-verify="sno" autocomplete="off"
                    class="layui-input">
            </div>
           <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <input type="text" name="mark" id="mark" autocomplete="off"  placeholder="请输入备注"
                       class="layui-input">
            </div>
             <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-inline">
                <input type="text" name="studentPhone" id="studentPhone" autocomplete="off"  placeholder="请输入电话" lay-verify="studentPhone"
                       class="layui-input">
            </div>
        </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">专业</label>
            <div class="layui-input-inline">
                <input type="text" name="speciality" id="speciality" autocomplete="off"  placeholder="请输入专业"
                       class="layui-input">
            </div>
        </div>


        </div>



        </div>
        <div class="layui-form-item layui-form-mid">
            <div class="layui-form-item">

            </div>
            <button class="layui-btn" lay-submit="" style="position:absolute;left:150px;"
                lay-filter="demo">确定</button>
        </div>
        <input hidden id="id" th:value="${id}">
    </form>

    <script>
    layui.use(['form', 'layedit', 'laydate'], function () {
            var id = $("#id").val()
            if (id>0) {
                var u = "/Stu/find?id=" + id;
                $.get(u, function (data, status) {
                    console.log(u)
                    var ret = JSON.parse(JSON.stringify(data));
                    if (ret.status == 1) {
                        alert(ret.msg);
                    } else {
                        $("#studentName").val(ret.data.studentName)
                          $("#className").val(ret.data.className)
                        $("#sno").val(ret.data.sno)
                            $("#mark").val(ret.data.mark)
                    $("#studentPhone").val(ret.data.studentPhone)
                        $("#speciality").val(ret.data.speciality)

                    }
                });
            }
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate;

            //创建一个编辑器
            var editIndex = layedit.build('LAY_demo_editor');

            //自定义验证规则
            var patrn = /^\+?[1-9][0-9]*$/;
            var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;

            form.verify({
                title: function (value) {
                    if (value = null || value.length == 0) {
                        return '学生姓名不能为空';
                    }
                }
/*                 , sno: function (value) {
                    if (value = null || value.length == 0||patrn.exec(value) == null ||value>200000000) {
                        return '只能小于20为非空数字';
                    }
                } */
                , studentPhone: function (value) {
                    if (myreg.exec(value) == null) {
                        return '请输入正确手机号码';
                    }
                }
                , content: function (value) {
                    layedit.sync(editIndex);
                }
            });
            //监听提交
            form.on('submit(demo)', function (data) {
        //Integer id, String name, String phone ,Integer countLimit

                var url;
                if (id==0) {
                    url = "/Stu/add?studentName=" + $("#studentName").val() + "&className=" + $("#className").val()+"&sno=" + $("#sno").val()+"&mark=" + $("#mark").val()+"&studentPhone=" + $("#studentPhone").val()+ "&speciality=" + $("#speciality").val()
                } else if (id>0) {
                    url = "/Stu/update?id=" + $("#id").val() + "&studentName=" + $("#studentName").val() + "&className=" + $("#className").val()+"&sno=" + $("#sno").val()+"&mark=" + $("#mark").val()+"&studentPhone=" + $("#studentPhone").val()+ "&speciality=" + $("#speciality").val()
                }
                console.log(url)
                var datas = JSON.parse(JSON.stringify(data.field));
                $.get(url, function (data, status) {
                    var ret = JSON.parse(JSON.stringify(data));
                    if (ret.status == 1) {
                        layer.alert(ret.msg);
                    } else {
                        layer.alert(ret.msg);
                        window.parent.location.reload();//刷新父页面
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭当前iframe页面
                        parent.location.reload();//刷新父页面
                    }

                });
                return false;
            });
        });
    </script>

</body>

</html>
